package leeon.bbsbrower.views;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

public class MailView extends ViewPart {
	public static final String ID = "bbsbrower.view.mail";

	private List list = null;

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		
		new Label(parent, SWT.NONE);
		
		//主菜单
		list = new List(parent, SWT.NONE);
		list.setFont(ApplicationWorkbenchWindowAdvisor.defaultFont);
		
		//写死
		list.add("阅览新信");
		list.add("所有信件");
		list.add("丢小纸条");
		
		//登陆进来时选中0
		list.select(0);
		
		//上下左右的时间
		list.addKeyListener(new KeyListener() {

			public void keyPressed(KeyEvent e) {
				//左
				if (e.keyCode == SWT.ARROW_LEFT) {
					actionToMain();
					e.doit = false;
				//右
				} else if (e.keyCode == SWT.ARROW_RIGHT || e.keyCode == 13) {										
					//新信
					if (list.getSelectionIndex() == 0) {
						actionNewMails();
					//所有信	
					} else if (list.getSelectionIndex() == 1) {
						actionAllMails();						
					//写信
					} else if (list.getSelectionIndex() == 2) {
						actionSendMail();
					}
					
					e.doit = false;
				}
			}

			public void keyReleased(KeyEvent e) {}
			
		});
		
		new Label(parent, SWT.NONE);
	}
	
	
	/**
	 * 回到主选单
	 */
	private void actionToMain() {		
		//跳转到主选单
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			MainView main = (MainView)page.showView(MainView.ID);
			main.refreshData(3);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 新信
	 */
	private void actionNewMails() {
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			MailListView mail = (MailListView)page.showView(MailListView.ID);
			mail.refreshData(0, 0, null);
		} catch (PartInitException e) {
			e.printStackTrace();
		}

	}
	
	/**
	 * 所有信
	 */
	private void actionAllMails() {
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			MailListView mail = (MailListView)page.showView(MailListView.ID);
			mail.refreshData(1, 0, null);
		} catch (PartInitException e) {
			e.printStackTrace();
		}

	}
	
	/**
	 * 发信
	 */
	private void actionSendMail() {
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			SendMailView mail = (SendMailView)page.showView(SendMailView.ID);
			mail.refreshData(0, null, null, null, 0);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 刷新数据
	 *
	 */
	public void refreshData(int index) {
		ApplicationActionBarAdvisor.setStatusLine();
		list.select(index);
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		list.setFocus();
	}
}